#include "system.h"

//volatile fifo* uartRXFifo;
int rxState; // 0-Preamble search,   1-Post-preamble


void initUart()
{
	//uartRXFifo = createFifo();
	initFifo();
	rxState = 0; //reset state to preamble search


	//Mode Register Data
	//_UFRZ			//Aparently not implemented in this chip
	_USIDL  = 0;	//Stop in Idle Mode Bit (Read/Write)
	_IREN   = 0;	//IrDA Encoder/Decoder Enable (Read/Write)
	_RTSMD  = 0;	//Simplex or Flow Control Modes (Read/Write)
	//_ALTO			//Apparently not implemented in this chip
	_UEN    = 0;	//Uart Function Enable Bits (Read/Write)
	_WAKE   = 0;	//Wake-up on Start Bit Dectect (Read/Write)
	_LPBACK = 0;	//Loopback Mode Select Bit (Read/Write)
	_ABAUD  = 0;	//Auto-Baud Enable Bit (Read/Write)
	_RXINV  = 0;	//Receive Polarity Inversion Bit (Read/Write)
	_BRGH   = 0;	//High Baud Rate Select (Read/Write)
	_PDSEL  = 0;	//Parity and Data Selection Bits (8 data, no parity) (Read/Write)
	_STSEL  = 0;	//Stop Selection (1 Stop Bit) (Read/Write)

	//Status Register Data
	_UTXISEL1 = 0;	//Transmission Interrupt Mode Bits (Read/Write)
	_UTXINV   = 0;	//Transmit Polarity Inversion Bit (Read/Write)
	_UTXBRK   = 0;	//Transmit Break Bit (Read/Write)
	//_UTXBF		//Transmit Buffer Full Status Bit (Read)
	//_TRMT			//Transmit Shift Register is Empty (Read)
	_URXISEL  = 0;	//Receive Interrupt Mode Selection Bits (Read/Write)
	_ADDEN    = 0;	//Address Character Detect Bit (Read/Write)
	//_RIDLE		//Receiver Idle Bit (Read)
	//_PERR			//Parity error Status Bit (Read)
	//_FERR			//Framing error Status Bit (Read)
	//_OERR			//Receive Buffer Overrun Error Status Bit (Read)
	//_URXDA		//Receive Buffer Data Available Bit (Read)

	//UART Receive Register
	//_U1RXREG		//Data Bits of the Received Character (Read)

	//UART Transmit Register
	//_U1TXREG		//Data Bits of the Transmitted Character (Write)

	//UART Baud Rate Generator Register
	U1BRG = 25;//21 for 6.8MHz FCLK//(SYSCLOCK/(16*UARTBAUD))-1; //The math of this function isn't working,
											 //so the value has been hand calculated and coded in

	//Uart enable bits
	_UARTEN = 1;	//Enable UART Part of Mode Register
	_UTXEN  = 1;	//Enable TX of UART Part of Status and Control Register
	_U1RXIF = 0;	//Clear Receive Interrupt Flag
	_U1RXIE = 1;	//Receive Interrupt Enable
	
}

//Transmit Functions
#ifdef RS485
void sendChar(char ch)
{
	int i;
//	LED2 = 1;
	do
	{
		while(_UTXBF || !_RIDLE);
		for (i=0; i<1000; i++);
		//LED2 = ~LED2;

	} while (_UTXBF || !_RIDLE);
	
	RS485RE_n = 1;
	RS485DE = 1;		
	U1TXREG = ch;
	while(!_TRMT);
	for(i=0;i<1000;i++);
	RS485RE_n = 0;
	RS485DE = 0;
	//LED2 = 0;
}
#endif

#ifdef RS232
void sendChar(char ch)
{
	while(_UTXBF);		
	U1TXREG = ch;
}
#endif

//sendCharArray
void sendCharArray(char *ch, int length)
{
	int index;
	for(index =0; index < length; index++)
	{
		//send the character
		sendChar(ch[index]);
	} 
}


//Receive Functions
int receiveChar(char* ch)
{
	return fifoRemove((byte *)ch);	
}


//Interrupt Functions
void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void)
{
	char ch;
	ch = U1RXREG;
	//sendChar(ch);
	
	if (rxState == 0)
	{
		//sendChar('0');
		//sendChar(0x0D);
		
		if ((ch&0xff) == 0xA3)
		{
			
			//sendChar('I');
			//sendChar(ch);
			if(!fifoInsert(ch))
			{
				//Some kind of error occured
			}
			rxState = 1;
		}
	}
	else if (rxState == 1)
	{
		
		//sendChar('1');
		//sendChar(0x0D);
		if(!fifoInsert(ch))
		{
			//Some kind of error occured
		}
	}
	
	_U1RXIF = 0;	//Reset Interrupt Flag
}

void __attribute__((__interrupt__, auto_psv)) _U1TXInterrupt(void)
{
	//Unused
}
